<?php
class metas extends spController
{
	public function index(){ // 分类列表
		$this->tpl_title = "分类管理";
		$conditions = "type='category'";
		$cate = spClass("lib_metas");
        $categorys = $cate->findAll($conditions);
		$treeObj = new Tree(array('mid','parent','name'));
		$this->results = $treeObj->getTree($categorys);		
		$this->display("admin/metas_index.html");
	  }	

	function write(){ // 撰写
		$this->tpl_title = "新建分类";
		$this->formUrl = spUrl("metas", "add");
		$this->parentId = $this->spArgs("parent");
		$conditions = "type='category'";
		$cate = spClass("lib_metas");
        $categorys = $cate->findAll($conditions);
		$treeObj = new Tree(array('mid','parent','name'));
		$this->results = $treeObj->getTree($categorys);
		$this->display("admin/metas_write.html");
	}	

	function edit(){ // 修改
		$this->tpl_title = "编辑分类";
		$this->formUrl = spUrl("metas", "update");
		$this->parentId = $this->spArgs("mid");
		$conditions = "type='category' and mid<>".$this->spArgs("mid");
		$cate = spClass("lib_metas");
        $categorys = $cate->findAll($conditions);
		$treeObj = new Tree(array('mid','parent','name'));
		$this->results = $treeObj->getTree($categorys);
		// 这里先判断是否传入了id
		if( $mid = $this->spArgs("mid") ){
			// 查找表，获取信息
			$this->info = spClass("lib_metas")->find(array('mid'=>$mid));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("metas","index"));
		}
		$this->display("admin/metas_edit.html");
	}
	  
	function add(){ // 增加分类
		$cate = spClass("lib_metas");
		$newrow = array( // 这里制作新增记录的值
			'parent' => $this->spArgs('parent'), 
			'name' => $this->spArgs('name'), 
			'slug' => $this->spArgs('slug'),
			'domain' => $this->spArgs('domain'),
			'sort' => $this->spArgs('sort'),
			'status' => $this->spArgs('status'),
			'thumb' => $this->spArgs('thumb'),
			'keywords' => $this->spArgs('keywords'),
			'description' => $this->spArgs('description'),
			'type' => 'category',
		);
		$cate->create($newrow); 
		$this->success("增加分类成功！", spUrl("metas", "index"));
	}

	function update(){ //更新
		$cate = spClass("lib_metas");
		if( $mid = $this->spArgs("mid") ){
		$conditions = array("mid"=>$mid); 
		$newrow = array(
			'parent' => $this->spArgs('parent'), 
			'name' => $this->spArgs('name'), 
			'slug' => $this->spArgs('slug'),
			'domain' => $this->spArgs('domain'),
			'sort' => $this->spArgs('sort'),
			'status' => $this->spArgs('status'),
			'thumb' => $this->spArgs('thumb'),
			'keywords' => $this->spArgs('keywords'),
			'description' => $this->spArgs('description'),
			);
		$cate->update($conditions, $newrow); // 更新记录
		$this->success("更新分类成功！", spUrl("metas", "index"));
		}
	}
	  
	function del(){// 删除
		// 这里先判断是否传入了id
		if( $mid = $this->spArgs("mid") ){
			// 执行删除
			spClass("lib_metas")->delete(array('mid'=>$mid));
			$this->success("删除成功！", spUrl("metas","index"));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("metas","index"));
		}
	}
	
}